//
//  DestinationViewController.m
//  
//
//  Created by xalo on 16/7/14.
//
//

#import "DestinationViewController.h"
#import "TableViewCell.h"
#import "PlayViewController.h"

#import "ShareViewController.h"

#import "ShoppingViewController.h"

#import "CustomCollectionViewCell.h"
#import "UITableView+EmptyData.h"

#import "HomePageModel.h"
#import "CityModel.h"
#import "SpotModel.h"
#import "MyMapViewController.h"

@interface DestinationViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *AttractionButt;
@property (weak, nonatomic) IBOutlet UIButton *NewButt;



@property (weak, nonatomic) IBOutlet UITableView *AttractionTableView;


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UITableView *imageTableView;
@property(nonatomic,strong)UIImageView *imageView;


@property(nonatomic,strong)UIButton *button;


@end
@interface CustomCollectionViewCell ()

@end

@implementation DestinationViewController



-(NSMutableArray *)cityArray{
    if (!_cityArray) {
        _cityArray = [NSMutableArray array];
    }
    return _cityArray;
}



-(NSMutableArray *)titleArray{
    if (!_titleArray) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell_city"];
    
    [self.AttractionTableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil]
                   forCellReuseIdentifier:@"table_cell"];
 
    
    [self requestProvinceDataSource];
    
   self.imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"235536365"]];
    
    self.imageView.alpha = 0.8;
    
        self.imageView.frame = CGRectMake(0, -kHeight/4, [UIScreen mainScreen].bounds.size.width, kHeight/4);

        self.imageView.contentMode = UIViewContentModeScaleAspectFill;
        self.imageTableView.contentInset = UIEdgeInsetsMake(kHeight/4, 0, 0, 0);
    self.imageTableView.separatorStyle = NO;
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.imageTableView addSubview:self.imageView];
    
    
  


}

//省份数据请求
- (void)requestProvinceDataSource{
    [RequestManager fetchDataWithUrl:@"http://www.roadqu.com/api/mobile/qunawan/city/provincelist" parameter:@{@"a_t":@"1468409445000",@"token":@"578624ce19a3f",@"sign":@"4b453e0843c2b7d0ba1023b77b7aeb6bbd5058bf"} method:@"POST" compeleted:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSMutableSet *set = bigDic[@"data"];
        for (NSDictionary *smallDic in set) {
            NSSet *districtSet = smallDic[@"provincelist"];
            for (NSDictionary *districtDIC in districtSet) {
                HomePageModel *model = [[HomePageModel alloc] init];
                [model setValuesForKeysWithDictionary:districtDIC];

                [self.titleArray addObject:model];

            }
        }
    } updateUI:^{
        HomePageModel *modol = self.titleArray[0];
        
        
        [self requestCityDataSource:@(modol.ID.integerValue) province:modol.name];

        
        [self.tableView reloadData];
    }];
}




#pragma make  tableView delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.tableView ) {
        
        return self.titleArray.count;
        
    }else if (tableView == self.AttractionTableView){
        [tableView tableViewDisplayMessage:@"加载中···" withSourceCount:self.cityArray.count];
    
    return self.cityArray.count;
    }else{
        
        return 1;
    }

}

//  实现方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
 
   
    if (tableView==self.tableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_city" forIndexPath:indexPath];
        
//        cell.backgroundColor = [UIColor colorWithRed:0.6 green:0.5 blue:0.6 alpha:0.6];

  
            cell.textLabel.text  = [self.titleArray[indexPath.row] name];
        
        cell.textLabel.font = [UIFont systemFontOfSize:15];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
          return cell;
        
    
    }else{
        
        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"table_cell" forIndexPath:indexPath];
        CityModel *modol =self.cityArray[indexPath.row];
        [cell setCellWithModol:modol];
        return cell;
    }
    

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.tableView) {
        
         HomePageModel *modol = self.titleArray[indexPath.row];
       

        [self requestCityDataSource:@(modol.ID.integerValue) province:modol.name];
        

    }else if  (tableView == self.AttractionTableView) {
        
        PlayViewController *playVC = [[PlayViewController alloc]init];
        
        SpotModel *modol = self.cityArray[indexPath.row];
        
        playVC.IDD = modol.ID;
        
        
        [self.navigationController pushViewController:playVC animated:YES];
        
        
   
        
    }
    
    

}


// 城市请求

- (void)requestCityDataSource:(NSNumber *)cityId province:(NSString *)province{
    
    [RequestManager fetchDataWithUrl:@"http://www.roadqu.com/api/mobile/qunawan/city/list" parameter:@{@"pid":cityId,@"token":@"578624ce19a3f"} method:@"POST" compeleted:^(NSData *data) {
        NSDictionary *BigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
        
        NSMutableSet *dataSet = BigDic[@"data"];
        
        self.cityArray = [NSMutableArray array];
        
        for (NSDictionary *smallDic in dataSet) {
            
            CityModel *model = [[CityModel alloc] init];
            
            [model setValuesForKeysWithDictionary:smallDic];
     
            [self.cityArray addObject:model];
            
        }
        
    } updateUI:^{

        
        [self.AttractionTableView reloadData];
        
    }];
}





-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableView) {
        return 50;
    }else{
        
    return 100;
    }

}





- (IBAction)NewButt:(id)sender {
    

    MyMapViewController *mapview = [[MyMapViewController alloc]init];
      self.hidesBottomBarWhenPushed =YES;
    
    [self.navigationController pushViewController:mapview animated:YES];
    
    self.hidesBottomBarWhenPushed = NO;


}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    if (point.y < -kHeight/4) {

        CGRect rect = self.imageView.frame;
        
        rect.origin.y = point.y;
        rect.size.height = self.imageView.frame.size.height - point.y;

        self.imageView.frame = rect;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
